// 函数的柯里化实现
// 实现函数add(1,2)(3)  add(1)(2)  add(2,4)(3,6)(4)
function add() {
    let sum = 0
    let fn = function () {
        Array.prototype.forEach.call(arguments, item => (sum += item))
        console.log(sum)
        return fn
    }
    fn(...arguments)
    return fn
}
let a = add(1, 2)(3, 4)(8)(4, 5, 6, 7)
/* let list = [1,3,4]
let sum = 0
Array.prototype.forEach.call(list,item=>(sum += item))
console.log(sum) */

function deepArr(arr) {
    let res = []
    for (let i = 0; i < arr.length; i++) {
        if (Object.prototype.toString.call(arr[i]) === '[object Array]') {
            res.push(deepArr(arr[i]))
        } else if (Object.prototype.toString.call(arr[i]) === '[object Object]') {
            res.push(deepClone(arr[i]))
        } else {
            res.push(arr[i])
        }
    }
    return res
}
function deepClone(obj) {
    let res = Object.create(null)
    let keys = Object.keys(obj)
    for (let i = 0; i < keys.length; i++) {
        let key = keys[i]
        let val = obj[key]
        //  这样会将数组拷贝成对象
        /* if(typeof(val) === 'object'){
            res[key] = deepClone(val)
        } */
        if (Object.prototype.toString.call(val) === '[object Object]') {
            res[key] = deepClone(val)
        } else if (Object.prototype.toString.call(val) === '[object Array]') {
            res[key] = deepArr(val)
        } else {
            res[key] = val
        }
    }
    return res
}
let obj = {
    name: {
        firstName: 'li',
        lastName: 'hua',
        info: [1, 10]
    },
    age: 20,
    list: [1, 4, 6],
    goal: [
        { math: 90 },
        { chinese: 100 }
    ]
}
let copy = deepClone(obj)
console.log(copy)


function getJSON (url) {

    return new Promise( (resolve, reject) => {
        var xhr = new XMLHttpRequest()
        xhr.open('GET', url, true)

        xhr.onreadystatechange = function () {
            if (this.readyState === 4) {
                if (this.status === 200) {
                    console.log('成功')
                    resolve(this.responseText)
                } else {
                    console.log('失败')
                    var resJson = { code: this.status, response: this.response }
                    reject(resJson, this)
                }
            }
        }

        xhr.send()
    })

}
// 使用promise封装一个原生的xhr请求
function ajax(url, data, method = 'GET') {
    return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest()
        if (method === 'GET') {
            //拼串
            let keys = Object.keys(data)
            for(let i = 0; i < keys.length; i++){
                if(i === 0) url = url + "?"+keys[i]+'='+data[keys[i]]
                else if(i!==(keys.length - 1)){
                    url = url + keys[i]+'='+data[keys[i]] + '&'
                }else{
                    url = url + keys[i]+'='+data[keys[i]]
                }
            }
            console.log(url)
            xhr.open(method, url, true)
            /* xhr.onreadystatechange = function () {
                if(this.readyState === 4 ){
                    // console.log(this)
                    let res = this.responseText
                    // console.log(res)
                    resolve(res)
                }else{
                    reject('error')
                }
                // console.log(this)
            } */
            xhr.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                        console.log('成功')
                        resolve(this.responseText)
                }
            }
            xhr.send()
        }
    })
}
let url = 'http://localhost:4000/jiaokaoquestions'
let data = {
    index: 4
}
async function axios(){
    let aj = await ajax(url, data, 'GET')
    console.log(aj)
}
axios()
/* getJSON('http://localhost:4000/jiaokaoquestions?index=5').then(res=>{
    console.log(res)
}) */




// 找到字符串中出现最少的字符
function sol(str){
	let arr = str.split('')
	//let res = {'a':2}
	let temp = arr.reduce((pre,cur,index,ar)=>{
		if(pre[cur]){
			pre[cur] += 1
		}else{	
			pre[cur] = 1
		}
		return pre
	},{})
	let keys = Object.keys(temp)
	let min = 1000
	let res = {}
	for(let i = 0; i < keys.length; i++){
		let key = keys[i]
		if(temp[key] < min){
			min = temp[key]
			//清空上一次的缓存数据
			res = {}
			res[key] = temp[key]
		}else if(temp[key] === min){
			res[key] = temp[key]
		}
	}
	return res 
}
function GetRes(str,obj){
	let res = []
	console.log(obj)
	let keys = Object.keys(obj)
	for(let i = 0; i < str.length; i++){
		let j
		for(j = 0; j < keys.length; j++ ){
			if(keys[j] !== str[i]){
			}else{
				break
			}
		}
		if(j === keys.length) res.push(str[i])
	}
	console.log('res',res.length)
	return res.join('')
}
let str = "aaabbbcceeff"
let obj = sol(str)
let res = GetRes(str,obj)
console.log(res)
// 转换成一个正则
//console.log(Object.keys(sol(str))[0])

//let waitS = Object.keys(sol(str))[0]

//let res = str.replace(waitS,'')
//console.log(res.join(''))

function Stack(){
	this.a = []
}
Stack.protoType = {
	mypop: function(){
		return this.a.pop()
	},
	mypush: function(a){
		return this.a.push(a)
	}
}
let stack = new Stack()
stack.mypush(1)


